package com.autotest.nore.common.utils;


import com.autotest.nore.domain.ReportCollectionCaseApi;
import com.autotest.nore.response.DebugRunResponse;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;

public class DebugResponseConverterUtils {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static DebugRunResponse convertToDebugRunResponse(ReportCollectionCaseApi report) {
        DebugRunResponse response = new DebugRunResponse();
        DebugRunResponse.DebugResult debugResult = new DebugRunResponse.DebugResult();
        DebugRunResponse.Request request = new DebugRunResponse.Request();
        DebugRunResponse.Response responseData = new DebugRunResponse.Response();

        // 设置请求信息
        request.setUrl(report.getApiPath());
        request.setHeaders(parseHeadersFromLog(report.getExecLog(), "请求头"));
        request.setBody(parseBodyFromLog(report.getExecLog(), "请求体"));

        // 设置响应信息
        responseData.setStatus(parseStatusFromLog(report.getExecLog()));
        responseData.setHeaders(parseHeadersFromLog(report.getExecLog(), "响应头"));
        responseData.setBody(parseBodyFromLog(report.getExecLog(), "响应体"));

        // 设置日志和控制台输出
        debugResult.setRequest(request);
        debugResult.setResponse(responseData);
        debugResult.setLogs(report.getExecLog());
        debugResult.setConsole(""); // 如果需要控制台输出，可以在这里设置
        // 设置新增字段
        response.setApiName(report.getApiName()); // 设置接口名称
        response.setApiPath(report.getApiPath()); // 设置接口路径
        response.setApiMethod(parseApiMethodFromLog(report.getExecLog())); // 设置接口方法
        response.setApiCaseName(report.getDescription()); // 设置接口用例名称（假设 description 是用例名称）
        response.setCaseApiIndex(report.getCaseApiIndex()); // 设置排序

        response.setDebugResult(debugResult);
        return response;
    }

    private static String parseHeadersFromLog(String execLog, String headerType) {
        Map<String, Object> headers = new HashMap<>();
        String headerKey = headerType + ":";
        int startIndex = execLog.indexOf(headerKey);
        if (startIndex != -1) {
            int endIndex = execLog.indexOf("<br><br>", startIndex);
            if (endIndex != -1) {
                String headerContent = execLog.substring(startIndex + headerKey.length(), endIndex).trim();
                return headerContent;
            }
        }
        return null;
    }
    private static String parseBodyFromLog(String execLog, String bodyType) {
        String bodyKey = bodyType + ":";
        int startIndex = execLog.indexOf(bodyKey);
        if (startIndex != -1) {
            int endIndex = execLog.indexOf("<br><br>", startIndex);
            if (endIndex != -1) {
                return execLog.substring(startIndex + bodyKey.length(), endIndex).trim();
            }
        }
        return null;
    }

    private static String parseStatusFromLog(String execLog) {
        String statusKey = "响应码:";
        int startIndex = execLog.indexOf(statusKey);
        if (startIndex != -1) {
            int endIndex = execLog.indexOf("<br><br>", startIndex);
            if (endIndex != -1) {
                return execLog.substring(startIndex + statusKey.length(), endIndex).trim();
            }
        }
        return null;
    }


    private static String parseApiMethodFromLog(String execLog) {
        String urlKey = "【请求信息】:<br><br>";
        int startIndex = execLog.indexOf(urlKey);
        if (startIndex != -1) {
            int endIndex = execLog.indexOf("<br><br>", startIndex + urlKey.length());
            if (endIndex != -1) {
                String urlLine = execLog.substring(startIndex + urlKey.length(), endIndex).trim();
                // 提取请求方法（如 POST、GET）
                // 假设日志格式为：时间戳 - Debug - 请求方法 URL
                String[] parts = urlLine.split(" ");
                if (parts.length >= 5) { // 确保日志格式正确
                    return parts[5]; // 请求方法在第 6 个位置
                }
            }
        }
        return null; // 如果未找到请求方法，返回 null
    }
}